
<?php
/***
 Class that uses HTTP to interact between the Javascript and the PHP/Database. General workflow
 involves a JQuery POST/GET to this file. The file then GETs the 'action' and calls the proper
 method based on the request. The function returns the information by echo or exiting any kind
 of text.
***/
include('../class_lib/cc_manager.php'); // Main accessor object

if (!isset($_GET['action'])){
	exit("No action specified");
}

switch ($_GET['action']) {
	case 'section':
		if (isset($_GET['department']) && isset($_GET['number'])) {
			get_course($_GET['department'], $_GET['number']);
		} else {
		    echo 'department & number required';
		}
		break;
	case 'get_rows':
		$cc = new cc_manager();
		$rows = $cc->get_rows($_GET['dept_code'], $_GET['course_num']);
		//echo print_r($rows);
		echo json_encode($rows);
		break;
	case 'get_rows_w_date':
		$cc = new cc_manager();
		$prof_array = isset($_GET['prof']) ? $_GET['prof'] : array();
		$rows = $cc->get_rows_with_date($_GET['dept_code'], $_GET['course_num'], $_GET['start'], $_GET['end'], $_GET['start_gpa'],$_GET['end_gpa'], $prof_array);
		echo json_encode($rows);
		break;
	case 'get_row':
		if (isset($_GET['sectionID'])) {
			$cc = new cc_manager();
			$row = $cc->get_row_from_section($_GET['sectionID']);
			echo json_encode($row);
		} else {
		    echo 'sectionID required';
		}	
		break;
	case 'get_terms':
		$cc = new cc_manager();
		$rows = $cc->get_terms();
		echo json_encode($rows);
		break;
	case 'get_cios_avgs':
		$cc = new cc_manager();
		$rows = $cc->get_cios_avgs();
		echo json_encode($rows);
		break;
	default:
		echo "No valid action specified";
}


/*
 * Function that returns JSON-Encoded message containing {code, gpa}
 * for all the courses given a department and a course number
 */
function get_course($dept, $num) {
	$cc = new cc_manager();
	echo json_encode($cc->get_course($dept, $num));	
}


?>